#pragma once
#include "Thread.h"
#include "QueueManager.h"

class Worker: public Thread
{
public:
	Worker(QueueManager<QueueElement>* _queue)
	{
		queue = _queue;
	};

	Worker(QueueManager<QueueElement>* _queue, int id) : Thread(PRIORITY_NORMAL, to_string(id))
	{
		queue = _queue;
	};

	~Worker(void){};

	void run()
	{
		while(true)
		{
			if(queue->size() > 0)
			{
				int u = queue->size();

				workingElement = queue->popFront();


				if(workingElement != NULL)
				{
					if(ACCEPT_EVENT == workingElement->eventType)
						workingElement->isInQueue = false;
					cout << endl << "Thread(" << getName() << "): Working on event type: " << workingElement->eventType << "." << endl;

					workingElement->eventhandler->handle_event(workingElement->eventhandler->get_handle(), workingElement->eventType);
					cout << endl << "Thread(" << getName() << "): Finished." << endl;
				}
			}
		
			else
			{
				Sleep(sleepTime);
			}
		}
	};

private:
	QueueManager<QueueElement>* queue;
	static const int sleepTime = 50;
	QueueElement* workingElement;
};

